/*
 @author Skitsanos
 */
var fffCommerce = {
	ui: {
		indicator: '<img src="images/ajax-loader.gif" alt="Loading..."/>'
	},
	models:{
		Category: function() {
			this.uid = null;
			this.parentUid = null;
			this.name = null;
		},
		Product: function() {
			this.uid = null;
			this.name = null;
			this.shortName = null;
			this.price = 0.0;
			this.description = null;
			this.thumbnail = null;
		}
	},
	events: {
		SHOW_LIST: '3fCommerce_showlist',
		SHOW_DETAILS: '3fCommerce_showdetails',
		ITEM_SELECTED: '3fCommerce_itemselected',
		CHECKOUT: '3fCommerce_checkout'
	},
	adapters:{
		persevere: {
			username: null,
			password: null,
			authenticate: function(options) {
				jQuery.ajax({
					type: 'POST',
					url: fffCommerce.api.url + '/Class/User',
					beforeSend: function(req) {
						req.setRequestHeader('Accept', 'application/json');
					},
					data: '{"method":"authenticate", "params":["' + fffCommerce.adapters.persevere.username + '","' + fffCommerce.adapters.persevere.password + '"],  "id":"call0"}',
					success: options.success,
					error: options.error
				});
			},
			call: function(options) {
				var query = '';
				if (options.query != undefined)
					query = options.query;
				jQuery.ajax({
					type: options.method,
					url: fffCommerce.api.url + '/' + options.objectType + '/' + query,
					beforeSend: function(req) {
						req.setRequestHeader('Accept', 'application/json');
					},
					data: JSON.stringify(options.data),
					success: options.success,
					error: options.error
				});
			}
		}
	},
	api: {
		url: 'http://192.168.182.145:8080',
		toString: function() {
			return 'v.1.0';
		},

		Categories: {
			create: function(data) {
			},
			update: function(data) {
			},
			remove: function(data) {
			},
			browse: function(query, callback) {
				var jsonpath = fffCommerce.api.url + '[?(@.name=\'' + escape(query) + '\')]';
				if (arguments.length == 0 || query == null || query == '')
					jsonpath = '';

				fffCommerce.adapters.persevere.call({
					method: 'GET',
					objectType: 'Category',
					query: jsonpath,
					success: function(e) {
						var listCategories = [];
						for (var item in e) {
							var cat = new fffCommerce.models.Category();
							cat.uid = e[item].id;
							cat.name = e[item].name;
							cat.parentUid = e[item].parentCategoryId;
							listCategories.push(cat);
						}
						if (callback)
							callback(listCategories);
					}
				});
			}
		},

		Products: {
			create: function(data) {
			},
			update: function(data) {
			},
			remove: function(data) {
			},
			browse: function(query, callback) {
				var _url = fffCommerce.api.url + '/Product/[?(@.short_name=\'' + query + '\')]';
				if (arguments.length == 0 || query == null || query == '')
					_url = fffCommerce.api.url + '/Product';
				$.ajax({type: 'GET',
					url: _url,
					beforeSend: function(req) {
						req.setRequestHeader('Accept', 'application/json');
					},
					success: function(e) {
						if ('error' in e) {
							callback(e);
						} else {
							var listProducts = [];
							for (var item in e) {
								var prod = new fffCommerce.models.Product();
								prod.uid = e[item].id;
								prod.name = e[item].long_name;
								prod.shortName = e[item].short_name;
								prod.price = e[item].price;
								prod.description = e[item].description;
								prod.thumbnail = e[item].photoThumbnailUrl;
								listProducts.push(prod);
							}
							callback({data: listProducts});
						}
					}
				});
			},
			getItem: function(query, callback) {
				var _url = fffCommerce.api.url + '/Product/' + query;
				$.ajax({type: 'GET',
					url: _url,
					beforeSend: function(req) {
						req.setRequestHeader('Accept', 'application/json');
					},
					success: function(e) {
						var listProducts = [];
						for (var item in e) {
							var prod = new fffCommerce.models.Product();
							prod.uid = e[item].id;
							prod.name = e[item].longName;
							prod.shortName = e[item].shortName;
							prod.price = e[item].price;
							prod.description = e[item].description;
							prod.thumbnail = e[item].photoThumbnailUrl;
							listProducts.push(prod);
						}
						if (callback)
							callback(e);
					}
				});
			}
		}
	}
};

$(function() {
	$('button,input:button,input:submit').button();

	$('#search').wrap('<div id="wrapperSearch" class="ui-widget"></div>');
	$('#search').addClass('ui-widget-content ui-corner-all').css({
		padding: '7px',
		width: '100%'
	});
	/*
	 $('#cart').addClass('ui-widget-content ui-corner-all').position({
	 my: "left top",
	 at: 'left top',
	 offset: '640 0',
	 of: '#wrapperSearch'
	 }).css({
	 width: '200px'
	 });

	 $('#list').position({
	 my: "left top",
	 at: "left bottom",
	 of: '#search'
	 }).css({
	 height: '200px'
	 });*/
});